<template>
	<view class="status-filter">
		<view class="status-scroll flex-row">
			<view class="status-item" v-for="(status, index) in statusTypes" :key="index"
				:class="{ 'tabcli':selectedStatus === index }" @tap="onTap(index)">
				{{ status }}
			</view>
		</view>
		<view class="select-tab status-scroll flex-row" style="padding:15rpx 0;">
			<template v-for="(status, index) in statusTypes" :key="index">
				<view class="item-tab">
					<view class="slect-item " v-if="selectedStatus === index"
						:class="{ 'active': selectedStatus === index }" @tap="onTap(index)">
						{{ status }}
					</view>
				</view>
				
			</template>
		</view>
	</view>
</template>

<script>
	import {
		getCurrentInstance
	} from "vue"
	const GestureState = {
		POSSIBLE: 0,
		BEGIN: 1,
		ACTIVE: 2,
		END: 3,
		CANCELLED: 4
	}
	const {
		Easing,
		spring,
		shared,
		timing,
		runOnUI,
		runOnJS,
		sequence
	} = wx.worklet
	const x = shared(0);
	const y = shared(0);
	const rotate = shared(0);
	const color = shared(0);
	const scale = shared(1);
	const pressed = shared(false);
	// 定义可以触发的事件
	const easing = Easing.ease
	export default {
		name: "bs-btntabs",
		props: {
			statusTypes: {
				type: Array,
				default: () => []
			},
			selectedStatus: {
				default: 0
			}
		},
		data() {
			return {

			};
		},
		mounted() {
			this.initApplyAnimatedCartStyle()
			this.headerAnimate()
		},
		watch: {
			selectedStatus: {
				handler(val) {
					let _this = this
					this.$nextTick(() => {
						// if(_this.scope){
						_this.headerAnimate()
						// }
					})
				},
				immediate: true,
				scope: true
			},
		},
		methods: {
			onTap(e) {
				this.$emit('onTap', e)
			},
			initApplyAnimatedCartStyle() {
				const scope = getCurrentInstance().proxy.$scope
				scope.applyAnimatedStyle('.select-tab', () => {
					'worklet'
					return {
						// backgroundColor: pressed.value ? '#5f9ea0' : '#adff2f',
						transform: `translateX(${x.value}px) translateY(${y.value}px) scale(${scale.value}) rotate(${rotate.value}deg)`,
					};
				}, {
					immediate: true,
					flush: 'async'
				}, (res) => {
					console.log('animatedStyle 已绑定到节点 ', res)

				})
				scope.applyAnimatedStyle('.item-tab', () => {
					'worklet'
					console.info('0 1px 8px 0px #fdb218${color.value}0',`0 1px 8px 0px #1a73e8${Math.floor(color.value) + ''}0`);
					return {
						// boxShadow:color.value==9?`0 1px 8px 0px #fdb21890`:'0 1px 8px 0px #fdb21810',
						boxShadow:`0 1px 8px 0px #fdb218${Math.floor(color.value) + ''}0`
					};
				}, {
					immediate: true,
					flush: 'async'
				}, (res) => {
					console.log('animatedStyle 已绑定到节点 ', res)
				
				})

			},
			async headerAnimate() {
				// console.log('getCurrentInstance',getCurrentInstance().proxy);
				// const scope = getCurrentInstance().proxy.$scope
				let _this = this
				const query = this.$scope.$vm.createSelectorQuery()
				query.select('.tabcli').boundingClientRect(function(res) {
					x.value = timing(res.left, {
						duration: 300,
						easing
					})
					
					color.value = sequence(timing(0, {
							duration: 100,
							easing
						}),
						timing(9, {
							duration: 200,
							easing
						}),
						timing(0, {
							duration: 200,
							easing
						})
					)
					scale.value = sequence(timing(0.8, {
							duration: 100,
							easing
						}),

						timing(1, {
							duration: 200,
							easing
						})
					)
				}).exec()

			},
		}
	}
</script>

<style lang="scss" scoped>
	.status-filter {
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
		position: relative;

		.select-tab {
			top: 0;
			left: 0;
			position: absolute;
		}

		.status-scroll {
			white-space: nowrap;
			padding: 15rpx 20rpx;

			.status-item {
				display: inline-block;
				padding: 10rpx 25rpx;
				margin-right: 15rpx;
				border-radius: 60rpx;
				font-size: 26rpx;
				color: #666;
				background-color: #f5f5f5;
				width: auto;

				&.active {
					background-color: #fdb218;
					color: #fff;
					backdrop-filter: blur(10px);
					// box-shadow: 0 2px 4px rgba(0,0,0,0.1);
				}
			}
		}
	}
	.slect-item {
		display: inline-block;
		padding: 10rpx 25rpx;
		border-radius: 60rpx;
		font-size: 26rpx;
		color: #666;
		background-color: #f5f5f5;
		width: auto;
	
		&.active {
			background-color: #fdb218;
			color: #fff;
			backdrop-filter: blur(10px);
			// box-shadow: 0 2px 4px rgba(0,0,0,0.1);
		}
	}
	.item-tab{
		display: inline-block;
		border-radius: 60rpx;
		font-size: 26rpx;
		color: #666;
		background-color: #f5f5f5;
		width: auto;
	}
</style>